from datetime import date

from fastapi import APIRouter, status

from app.crud.plans import (
    approve_study_plan,
    approve_study_plans_by_ids,
    complete_study_plan,
    complete_study_plans_by_ids,
    create_study_plan,
    get_all_study_plans_by_period,
    get_all_study_plans_by_year,
    get_study_plans_by_year_and_month,
    update_study_plan,
)
from app.models.plans import StudyPlanStatus
from app.schemas.plans import (
    StudyPlanCreateSchema,
    StudyPlanSchema,
    StudyPlanUpdateSchema,
)

router = APIRouter()


@router.get("/status/", response_model=list[str])
async def get_study_plan_statuses_():
    """Get all study plan statuses"""
    return [status.value for status in StudyPlanStatus]


@router.post("/", response_model=StudyPlanSchema, status_code=status.HTTP_201_CREATED)
async def create_study_plan_(study_plan_schema: StudyPlanCreateSchema):
    """Create a new study plan"""
    return await create_study_plan(study_plan_schema)


@router.put("/", response_model=StudyPlanSchema)
async def update_study_plan_(study_plan_update_schema: StudyPlanUpdateSchema):
    """Update a study plan"""
    return await update_study_plan(study_plan_update_schema)


@router.get("/", response_model=list[StudyPlanSchema])
async def get_study_plans_by_year_and_month_(
    year: int,
    month: int | None = None,
):
    """Get all study plans by year and month"""
    if month:
        return await get_study_plans_by_year_and_month(year, month)
    return await get_all_study_plans_by_year(year)


@router.get("/period/", response_model=list[StudyPlanSchema])
async def get_study_plans_by_period_(
    start_year: int,
    start_month: int,
    end_year: int | None = None,
    end_month: int | None = None,
):
    """Get all study plans by period"""
    if end_year is None:
        end_year = date.today().year
    if end_month is None:
        end_month = date.today().month
    return await get_all_study_plans_by_period(start_year, start_month, end_year, end_month)


@router.patch("/approve/", response_model=StudyPlanSchema)
async def approve_study_plan_(study_plan_id: int):
    """Approve a study plan"""
    return await approve_study_plan(study_plan_id)


@router.patch("/approve/multiple/", response_model=list[StudyPlanSchema])
async def approve_study_plans_by_ids_(study_plan_ids: list[int]):
    """Approve multiple study plans"""
    return await approve_study_plans_by_ids(study_plan_ids)


@router.patch("/complete/", response_model=StudyPlanSchema)
async def complete_study_plan_(study_plan_id: int):
    """Complete a study plan"""
    return await complete_study_plan(study_plan_id)


@router.patch("/complete/multiple/", response_model=list[StudyPlanSchema])
async def complete_study_plans_by_ids_(study_plan_ids: list[int]):
    """Complete multiple study plans"""
    return await complete_study_plans_by_ids(study_plan_ids)


@router.get("/forecast/", response_model=list[StudyPlanSchema])
async def get_forecasted_study_plans_(year: int, month: int):
    """Get all forecasted study plans"""
    # MOCK. Needs implementation
    return await get_study_plans_by_year_and_month(year, month)
